<template>
  <div class="kong">
    <el-dialog
      v-model="finishExpensesVisible"
      title="完成花销"
      width="40%"
      :close-on-click-modal="false"
      :before-close="handelClose"
      center
    >
      <div>
        <el-form ref="finishExpensesRef" :rules="rules" :model="queryReq">
          <el-form-item label="完成时间" prop="finishTime">
            <el-date-picker
              type="date"
              placeholder="输入打卡时间"
              v-model="queryReq.finishTime"
            />
          </el-form-item>
          <el-form-item label="实际金额" prop="realAmount">
            <el-input-number
              v-model="queryReq.realAmount"
              :min="0"
              :max="10000000"
            />
            &nbsp;￥
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input v-model="queryReq.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel(finishExpensesRef)">取 消</el-button>
          <el-button type="primary" @click="submitAddIt(finishExpensesRef)"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs } from 'vue'
import { ElMessage, FormRules, FormInstance } from 'element-plus'
import { finishIt } from '@/api/expenses'

const finishExpensesRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  finishTime: [
    {
      required: true,
      message: '请输入完成时间',
      trigger: 'change',
    },
  ],
  realAmount: [
    {
      required: true,
      message: '请输实际金额',
      trigger: 'change',
    },
  ],
})

let queryReq: any = reactive({
  planId: '',
  finishTime: '',
  realAmount: '',
  remark: '',
})

interface Props {
  finishExpensesVisible?: Boolean
}

const myProps = defineProps<Props>()

const { finishExpensesVisible } = toRefs(myProps)

const emit = defineEmits(['finishExpensesConfig'])

const changeStatus = (status: boolean, formEl: FormInstance | undefined) => {
  resetFields(formEl)
  emit('finishExpensesConfig', status)
}

const resetFields = (formEl: FormInstance | undefined) => {
  if (formEl) {
    formEl.resetFields()
  } else {
    finishExpensesRef.value?.resetFields()
  }
}

const cancel = (formEl: FormInstance | undefined) => {
  changeStatus(false, formEl)
  resetFields(formEl)
}

const handelClose = (done: () => void, formEl: FormInstance | undefined) => {
  changeStatus(!finishExpensesVisible, formEl)
  done()
}

const submitAddIt = async (formEl: FormInstance | undefined) => {
  console.log('submitAddIt')
  if (!formEl) {
    return
  }

  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      finishIt(queryReq).then((res: any) => {
        if (res.data) {
          changeStatus(!finishExpensesVisible, formEl)
          ElMessage.success('花销设为完成状态')
        }
      })
    }
  })
}

const initsObj = (rows: any) => {
  if (rows) {
    queryReq.planId = rows.id
  }
}

defineExpose({
  initsObj,
})
</script>

<style lang="scss" scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
